package com.app.micaihu.bean.news;

import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.ad.toutiao.ToutiaoAdManager;
import com.app.micaihu.ad.toutiao.ToutiaoCloseDialog;
import com.app.micaihu.ad.toutiao.ToutiaoNativeAd;
import com.app.micaihu.bean.ad.NativeAd;
import com.blankj.utilcode.util.LogUtils;
import com.bytedance.sdk.openadsdk.FilterWord;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;

import java.util.List;


/**
 * 头条
 * Created by linkai on 2017/3/17.
 */
public class ViewToutiaoAd implements View.OnClickListener {

    public FrameLayout view_express;//

    public ViewToutiaoAd(View convertView) {
        initView(convertView);
    }

    /**
     * 初始化控件
     *
     * @param convertView
     */
    public void initView(View convertView) {
        this.view_express = convertView.findViewById(R.id.view_express);
        convertView.setTag(this);
    }

    /**
     * 加载数据
     *
     * @param newsEntity
     */
    public void loadData(NewsEntity newsEntity, boolean flushToutiao) {
        NativeAd nativeAd = newsEntity.getNativeAd();
        if (nativeAd == null) return;
        if (!flushToutiao) return;
        view_express.setVisibility(View.VISIBLE);
        view_express.removeAllViews();
        int nativeAdSize = 0;
        // 如果没有获取到广告则 从广告管家里获取一个新广告
        ToutiaoNativeAd adManager = ToutiaoAdManager.getInstance().getAd(nativeAd.getAdId());
        if (adManager == null) {
            ToutiaoAdManager.getInstance().putAd(nativeAd);
        } else {
            nativeAdSize = adManager.getNativeAdSize();
        }
//        if (!flushToutiao) {
//            TTNativeExpressAd ttNativeExpressAd = ttNativeExpressAdSparseArray.get(position);
//            if (ttNativeExpressAd != null) {
//                fillToutiaoView(viewHolder, position, newsEntity, ttNativeExpressAd);
//                return;
//            }
//        }
//        LogUtils.e("------------------------------------------------->" + position + " flushToutiao->" + flushToutiao);
        // 如果广告缓存中有广告则开始获取
        if (nativeAdSize > 0) {
            TTNativeExpressAd nrAd = adManager.getNativeAd();
            // 如果获取的广告不为null
            if (nrAd != null) {
                fillToutiaoView(newsEntity, nrAd);
            }
        } else {
            String ad = "";
            //用获取到的广告id去获取预加载的广告
            if (!TextUtils.isEmpty(ad)) {
                ToutiaoNativeAd bna = ToutiaoAdManager.getInstance().getAd(ad);
                if (bna != null) {
                    TTNativeExpressAd oneNrAd = bna.getNativeAd();
                    // 如果获取的广告不为null
                    if (oneNrAd != null) {
                        fillToutiaoView(newsEntity, oneNrAd);
                        return;
                    }
                }
            }
            //如果最终还是没有获取到加载好的广告,则通过广告id去请求广告
            ToutiaoNativeAd bna = ToutiaoAdManager.getInstance().getAd(nativeAd.getAdId());
            if (bna != null) {
                bna.loadOneAd(view_express.getContext(), nativeAd.getAdId(), new ToutiaoNativeAd.NativeAdLoadCallback() {

                    @Override
                    public void onNativeAdLoad(TTNativeExpressAd ad, boolean success) {
                        if (ad != null && success) {
                            fillToutiaoView(newsEntity, ad);
                        }
                    }
                });
            }
        }
    }

    private void fillToutiaoView(NewsEntity newsEntity, TTNativeExpressAd nrAd) {
        if (newsEntity == null || nrAd == null) return;
//        ttNativeExpressAdSparseArray.put(position, nrAd);
        delDialogView(newsEntity, nrAd);
        View expressAdView = nrAd.getExpressAdView();
        if (expressAdView != null && expressAdView.getParent() == null) {
            view_express.addView(expressAdView);
//            nrAd.render();
        }
    }

    private void delDialogView(NewsEntity newsEntity, TTNativeExpressAd adView) {
        //使用自定义样式
        List<FilterWord> words = adView.getFilterWords();
        if (words == null || words.isEmpty()) {
            return;
        }
        ToutiaoCloseDialog toutiaoCloseDialog = new ToutiaoCloseDialog(view_express.getContext(), words);
        toutiaoCloseDialog.setOnDislikeItemClick(new ToutiaoCloseDialog.OnDislikeItemClick() {
            @Override
            public void onItemClick(FilterWord filterWord) {
//                LogUtils.e("toutiao filterWord", filterWord.getName(), "del position->" + position);
//                LiveEventBus.get(Config.EventBus.delNewsListAd, Integer.class).post(position);
//                list.remove(newsEntity);
//                notifyDataSetChanged();
                view_express.setVisibility(View.GONE);
            }
        });
        toutiaoCloseDialog.setCancelable(true);
        toutiaoCloseDialog.setCanceledOnTouchOutside(true);
        adView.setDislikeDialog(toutiaoCloseDialog);
    }

    @Override
    public void onClick(View v) {
    }

}
